package com.qf.zsq.controller;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.qf.entity.GoodsEntity;
import com.qf.entity.GoodsTypeEntity;
import com.qf.finalstr.FruitsFinal;
import com.qf.util.StringUtil;
import com.qf.zsq.service.IGoodsService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import java.util.List;

@Controller
public class GoodsController {

    @Autowired
    IGoodsService goodsService;

    @RequestMapping("index")
    public String index(HttpServletRequest request){

        //查询商品列表查询8个商品
        PageHelper.startPage(FruitsFinal.NUM_ONE,FruitsFinal.NUM_EIGHT);
        List<GoodsEntity> goodsList = goodsService.queryGoodsList(null);

        request.setAttribute("goodsList",goodsList);

        //2: 查询商品的类别列表
        PageHelper.startPage(FruitsFinal.NUM_ONE,FruitsFinal.NUM_THREE);
        List<GoodsTypeEntity> goodsTypeList = goodsService.queryGoodsTypeList();

        request.setAttribute("goodsTypeList",goodsTypeList);

        return "index";
    }

    //根据条件查询商品
    @RequestMapping("queryGoods")
    public String queryGoods(GoodsEntity ge,HttpServletRequest request){

        //当前页,页面记录数
        PageHelper.startPage(ge.getPageNum(),ge.getPageSize());

        //根据条件去查询商品信息
        List<GoodsEntity> list = goodsService.queryGoodsList(ge);

        //做分页
        PageInfo<GoodsEntity> pageInfo = new PageInfo<>(list);

        request.setAttribute("pageInfo",pageInfo);
        request.setAttribute("goodsInfo",ge);
        return "prolist";
    }

    @RequestMapping("queryGoodsDetail")
    public String queryGoodsDetail(String goodsId,HttpServletRequest request){

        if (StringUtil.isEmpty(goodsId)) {
            return "error";
        }

        GoodsEntity ge =goodsService.queryGoodsDetail(goodsId);

        request.setAttribute("goodsEntity",ge);

        return "product";
    }

    /**
     * 跳转到购物车
     * @return
     */
    @RequestMapping("jumpShopCar")
    public String jumpShopCar(){
        return "shopcart";
    }

    /**
     * 清空购物车
     * @param session
     * @return
     */
    @RequestMapping("clean")
    public String clean(HttpSession session){
        session.removeAttribute("shopCar");
        session.removeAttribute("msg");
        return "redirect:jumpShopCar";
    }

}
